/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ins.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.ins.entity.InsField;
import com.thinkgem.jeesite.modules.ins.entity.InsTableTemplate;
import com.thinkgem.jeesite.modules.ins.service.InsFieldService;
import com.thinkgem.jeesite.modules.ins.service.InsTableTemplateService;

/**
 * 数据表模板Controller
 * @author fanqinglong
 * @version 2018-05-30
 */
@Controller
@RequestMapping(value = "${adminPath}/ins/insTableTemplate")
public class InsTableTemplateController extends BaseController {
	@Autowired
	private InsFieldService insFieldService;
	
	@Autowired
	private InsTableTemplateService insTableTemplateService;
	
	@ModelAttribute
	public InsTableTemplate get(@RequestParam(required=false) String id) {
		InsTableTemplate entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = insTableTemplateService.get(id);
		}
		if (entity == null){
			entity = new InsTableTemplate();
		}
		return entity;
	}
	
	@RequiresPermissions("ins:insTable:edit")
	@RequestMapping(value = "getByName")
	@ResponseBody
	public String getByName(InsTableTemplate insTableTemplate){
		String result = "false";
		InsTableTemplate resultTableTemplate = insTableTemplateService.getByName(insTableTemplate);
		if (null == resultTableTemplate || resultTableTemplate.getId().equals(insTableTemplate.getId())) {
			result = "true";
		}
		return result;
	}
	
	@RequiresPermissions("ins:insTable:view")
	@RequestMapping(value = {"list", ""})
	public String list(InsTableTemplate insTableTemplate, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<InsTableTemplate> page = insTableTemplateService.findPage(new Page<InsTableTemplate>(request, response), insTableTemplate); 
		model.addAttribute("page", page);
		model.addAttribute("insTableTemplate", insTableTemplate);
		return "modules/ins/insTableTemplateList";
	}

	@RequiresPermissions("ins:insTable:view")
	@RequestMapping(value = "form")
	public String form(InsTableTemplate insTableTemplate, Model model) {
		InsField param = new InsField();
		param.setFrequently("1");
		List<InsField> list = insFieldService.findList(param);
		model.addAttribute("fieldList", list);
		model.addAttribute("insTableTemplate", insTableTemplate);
		return "modules/ins/insTableTemplateForm";
	}

	@RequiresPermissions("ins:insTable:edit")
	@RequestMapping(value = "save")
	public String save(InsTableTemplate insTableTemplate, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, insTableTemplate)){
			return form(insTableTemplate, model);
		}
		insTableTemplateService.save(insTableTemplate);
		addMessage(redirectAttributes, "保存模板成功");
		return "redirect:"+Global.getAdminPath()+"/ins/insTableTemplate/?repage";
	}
	
	@RequiresPermissions("ins:insTable:edit")
	@RequestMapping(value = "delete")
	public String delete(InsTableTemplate insTableTemplate, RedirectAttributes redirectAttributes) {
		insTableTemplateService.delete(insTableTemplate);
		addMessage(redirectAttributes, "删除模板成功");
		return "redirect:"+Global.getAdminPath()+"/ins/insTableTemplate/?repage";
	}

}